using System;
using System.Net.Mail;

namespace ADEVSO.Net
{
    public class Mail
    {
        #region Variables

        private SmtpClient mSmtpClient;
        private String mSmtpServer;
        private String mUser;
        private String mPassword;
        private String mFrom;
        private String mTo;
        private String mCC;
        private String mBCC;
        private String mSubject;
        private String mBody;
        private int mPort;

        #endregion

        #region Contructors

        /// <summary>
        /// Contructor
        /// </summary>
        public Mail()
        {
            
        }

        #endregion

        #region Properties

        public String SmtpServer
        {
            get { return mSmtpServer; }
            set { mSmtpServer = value; }
        }

        public String User
        {
            get { return mUser; }
            set { mUser = value; }
        }

        public String Password
        {
            get { return mPassword; }
            set { mPassword = value; }
        }

        public String From
        {
            get { return mFrom; }
            set { mFrom = value; }        
        }

        public String To
        {
            get { return mTo; }
            set { mTo = value; }
        }

        public String CC
        {
            get { return mCC; }
            set { mCC = value; }
        }

        public String BCC
        {
            get { return mBCC; }
            set { mBCC = value; }
        }

        public String Subject
        {
            get { return mSubject; }
            set { mSubject = value; }
        }

        public String Body
        {
            get { return mBody; }
            set { mBody = value; }
        }

        public int Port
        {
            get { return mPort; }
            set { mPort = value; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Create SmtpClient
        /// </summary>
        private void SetSmtpClient()
        {
            mSmtpClient = new SmtpClient(SmtpServer);
            if (User != null && Password != null)
            {
                mSmtpClient.UseDefaultCredentials = false;
                mSmtpClient.Credentials = new System.Net.NetworkCredential(User, Password);
            }
            mSmtpClient.Port = Port;
            mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        }

        /// <summary>
        /// Send Email
        /// </summary>
        public int Send()
        {
            // Set SmtpClient
            SetSmtpClient();

            // Create Mail Message
            MailMessage message = new MailMessage();
            message.From = new MailAddress(From);
            message.To.Add(To);
            if (CC != null)
                message.CC.Add(CC);
            if (BCC != null)
                message.Bcc.Add(BCC);
            message.Subject = Subject;
            message.Body = Body;
            message.IsBodyHtml = true;

            try
            {
                // Send Email
                mSmtpClient.Send(message);
                return 1;
            }
            catch
            {
                return 0;
            }
        }

        #endregion
    }
}
